package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.common.core.domain.BaseEntity;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.TimeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 预约信息Service业务层处理
 *
 * @author alexis
 * @date 2025-03-05
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id) {
        return reservationMapper.selectById(id);
    }

    /**
     * 查询预约信息列表
     *
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation) {
        reservation.setCreateBy(UserThreadLocal.get()+"");
        reservation.setStatus(0);
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation) {
        LocalDateTime time = reservation.getTime();
        if (time.isBefore(LocalDateTime.now())) {
            throw new BaseException("预约时间不能早于当前时间");
        }
        if (this.lambdaQuery().ne(Reservation::getStatus,2).eq(Reservation::getTime, time).count() >= 6) {
            throw new BaseException("当前时间段预约人数已满");
        }
        if (this.lambdaQuery().ne(Reservation::getStatus,2).eq(Reservation::getTime, time).
                eq(Reservation::getMobile, reservation.getMobile()).count() > 0) {
            throw new BaseException("一个手机号在同一个时间段内只能预约一次");
        }
        reservation.setStatus(0);
        reservation.setCreateBy(UserThreadLocal.get() + "");
        return reservationMapper.insert(reservation);
    }

    /**
     * 修改预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation) {
        return reservationMapper.updateById(reservation);
    }

    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids) {
        return reservationMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除预约信息信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id) {
        return reservationMapper.deleteById(id);
    }

    /**
     * 查询当天取消预约数量
     *
     * @return 结果
     */
    @Override
    public Long cancelledCount() {
        LocalDateTime start = LocalDate.now().atStartOfDay();
        LocalDateTime end = start.plusDays(1);
        return this.lambdaQuery().eq(Reservation::getStatus, 2).
                eq(BaseEntity::getUpdateBy, UserThreadLocal.get()).
                between(BaseEntity::getUpdateTime, start, end).
                count();
    }

    /**
     * 查询每个时间段剩余预约次数
     */
    @Override
    public List<TimeVo> countByTime(Long time) {
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        return reservationMapper.countByTime(startTime, endTime);
    }

    /**
     * 取消预约
     */
    @Override
    public void cancel(Long id) {
        Reservation reservation = new Reservation();
        reservation.setStatus(2);
        reservation.setId(id);
        this.updateById(reservation);
    }

    /**
     * 定时更新预约状态
     */
    @Override
    @Transactional
    public void updateReservationStatus() {
        List<Reservation> list = this.lambdaQuery().eq(Reservation::getStatus, 0).lt(Reservation::getTime, LocalDateTime.now()).list();
        list.forEach(v->v.setStatus(3));
        updateBatchById(list);
    }
}
